﻿using System;
using System.Net.Http;

namespace Qiao.Common.Core
{
    /// <summary>
    /// 封装的HttpClient
    /// </summary>
    public class HttpClientHelper
    {
        #region Post请求
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="requestUrl">请求地址</param>
        /// <param name="content">请求上下文</param>
        /// <returns></returns>
        public static HttpResponseMessage Post(string requestUrl, HttpContent content, int timeOut = 60)
        {
            var response = new HttpResponseMessage();
            using (var http = new HttpClient())
            {
                http.Timeout = new TimeSpan(0, 0, timeOut);
                try
                {
                    response = http.PostAsync(requestUrl, content).Result;
                    return response;
                }
                catch (Exception)
                {
                    try
                    {
                        response = http.PostAsync(requestUrl, content).Result;
                        return response;
                    }
                    catch (Exception ex)
                    {
                        response = new HttpResponseMessage(System.Net.HttpStatusCode.RequestTimeout) { Content = new StringContent("请求超时") };
                        return response;
                    }
                }
            }
        }

        #endregion

        #region Get请求
        /// <summary>
        /// Get请求 
        /// </summary>
        /// <param name="requestUrl">请求地址</param>
        /// <returns></returns>
        public static HttpResponseMessage Get(string requestUrl, int timeOut = 60)
        {
            var response = new HttpResponseMessage();
            using (var http = new HttpClient())
            {
                http.Timeout = new TimeSpan(0, 0, timeOut);
                //http.DefaultRequestHeaders.Add("Accept-Encoding", "GB2312");
                //http.DefaultRequestHeaders.Add("Accept-Language", "zh-CN,zh;q=0.8");
                //http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
                try
                {
                    response = http.GetAsync(requestUrl).Result;
                    return response;
                }
                catch (Exception)
                {
                    try
                    {
                        response = http.GetAsync(requestUrl).Result;
                        return response;
                    }
                    catch (Exception ex)
                    {
                        response = new HttpResponseMessage(System.Net.HttpStatusCode.RequestTimeout) { Content = new StringContent("请求超时") };
                        return response;
                    }
                }
            }
        }
        #endregion

        #region Put请求
        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="requestUrl">请求地址</param>
        /// <param name="content">请求上下文</param>
        /// <returns></returns>
        public static HttpResponseMessage Put(string requestUrl, HttpContent content, int timeOut = 60)
        {
            var response = new HttpResponseMessage();
            using (var http = new HttpClient())
            {
                http.Timeout = new TimeSpan(0, 0, timeOut);
                try
                {
                    response = http.PutAsync(requestUrl, content).Result;
                    return response;
                }
                catch (Exception)
                {
                    try
                    {
                        response = http.PutAsync(requestUrl, content).Result;
                        return response;
                    }
                    catch (Exception)
                    {
                        response = new HttpResponseMessage(System.Net.HttpStatusCode.RequestTimeout)
                        {
                            Content = new StringContent("请求超时")
                        };
                    }
                    return response;
                }
            }
        }
        #endregion

        #region Delete请求
        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <returns></returns>
        public static HttpResponseMessage Delete(string requestUrl, int timeOut = 60)
        {
            var response = new HttpResponseMessage();
            using (var http = new HttpClient())
            {
                http.Timeout = new TimeSpan(0, 0, timeOut);
                try
                {
                    response = http.DeleteAsync(requestUrl).Result;
                    return response;
                }
                catch (Exception)
                {
                    try
                    {
                        response = http.DeleteAsync(requestUrl).Result;
                        return response;
                    }
                    catch (Exception)
                    {
                        response = new HttpResponseMessage(System.Net.HttpStatusCode.RequestTimeout)
                        {
                            Content = new StringContent("请求超时")
                        };
                    }
                    return response;
                }
            }
        }
        #endregion
    }
}

